﻿//订单信息 党明 2012/4/30
var coupon = "";
var addressId = 0;
var couponprice = 0;
$(document).ready(function () {
    //加载信息
    userReciveInfo();
    payinfo();
    orderinfo();

    orderfee();
    //绑定下订单事件
    $("#btnGoShopping").bind("click", function () {
        window.location.href = "/index.html";
    });
    $("#btnPay").bind("click", SubmitOrder);

    $("#btnAddUserAddress").bind("click", ShowAddUserAddressForm);
    //
    $("#addressU").bind("click", ShowAddressForm);
    $("#btnFindCoupon").bind("click", findCoupon);
});

function ShowAddressForm() {
    var url = $(this).attr("tag");
    if (!url)
        return;
    $("#divReciveInfo").hide();
    $("#divaddressU").show();

    $("#divaddressU").load(url);
}

function CancelCoupon() {
    $("input[name='usercoupon']").each(function () {
        $(this).attr("checked", false);
    });
    $("#couponid").val("");
    coupon = "";
}
function CalcOrderPrice(price) {
    coupon = $("#couponid").val();
    couponprice = price;
    $(".orderfee_coupon").html(couponprice);
    $(".totalprice").html((orderpriceinfo.goodsprice + orderpriceinfo.sendfee - couponprice));
}

function findCoupon() {

    if ($.trim($("#couponid").val()) == "") {
        alert("请输入优惠券编码");
        return;
    }
    var action = "findCoupon";
    var success = function (ret) {
        if (ret) {
            eval("ret=" + ret);

            $(".couponinfo").html("<font color='blue'>优惠券信息：面值：" + parseInt(ret.Coupon._couponmoney) + "；备注:" + ret.UserCoupon._memo + "</font> [<input type='checkbox' id='usercoupon' coupon='" + parseInt(ret.Coupon._couponmoney) + "' onclick='CalcOrderPrice(" + parseInt(ret.Coupon._couponmoney) + ");'>使用]");
        }
    }
    OrderInfoAjaxPost(action, success, "GET", "couponid=" + $("#couponid").val());
}

function SubmitOrder() {

    var data = Array();
    data.push("fapiao=" + encodeURI($("#fapiao").val()));
    data.push("coupon=" + encodeURI(coupon));
    data.push("paymode=" + encodeURI($("input[name='paymode']:checked").val()));

    data.push("remark=" + encodeURI($("#remark").val()));
    data.push("addressId=" + addressId);
    data.push("sendmode=" + encodeURI($("input[name='sendmode']:checked").val()));
    var success = function (ret) {
        eval("ret=" + ret);
        if (ret && ret.Result) {
            window.location.href = "/home/Myorders.aspx";
        }
    }
    OrderInfoAjaxPost("submitorder", success, "GET", data.join("&"));
}
var orderpriceinfo;
function orderfee() {

    var action = "orderFee";

    var success = function (ret) {
        if (ret) {
            eval("ret=" + ret);
            orderpriceinfo = ret;
            var html = "商品总价：￥" + ret.goodsprice + "元";
            html += ";运费：" + ret.sendfee + "元";
            html += ";使用优惠券：<span class='orderfee_coupon'>" + couponprice + "</span>元;共计<span class='totalprice'>" + (ret.goodsprice + ret.sendfee - couponprice) + "</span>元";
            $("#divcalprice>.orderinfo").html(html);
        }
    }
    var data = Array();
    data.push("addressId=" + addressId);
    data.push("sendmode=" + $("input[name='sendmode']:checked").val());
    OrderInfoAjaxPost(action, success, "GET", data.join("&"));
}
function ShowAddUserAddressForm() {
    var action = "userReciveInfo";
    var success = function (ret) {
        if (ret) {
            //把添加用户地址的form展示出来
        }
    }
    OrderInfoAjaxPost(action, success);
}
//添加用户地址的事件
function AddUserAddress() {

}

function userReciveInfo() {
    var action = "userReciveInfo";
    var success = function (ret) {
    }
    OrderInfoAjaxPost(action, success, "GET");
}

function payinfo() {
    var action = "payinfo";
    var success = function (ret) {
        $("#divPayInfo>.orderinfo").html(ret);
    }
    OrderInfoAjaxPost(action, success, "GET");
}

function orderinfo() {
    var action = "orderinfo";
    var success = function (ret) {
        $("#divgoodsinfo>.orderinfo").html(ret);
    }
    OrderInfoAjaxPost(action, success, "GET");
}

function coupon() {
    var action = "coupon";
    var success = function (ret) {
    }
    OrderInfoAjaxPost(action, success, "GET");
}

function OrderInfoAjaxPost(action, success1, posttype, data) {
    var url = "/home/as/OrderInfoCommon.aspx?Action=" + action + "&r=" + Math.random();

    $.ajax(
    {
        type: posttype,
        url: url,
        data: data,
        success: success1
    });
}